package com.example.management;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.management.api.ApiRequest;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class AddOrUpdateActivity extends AppCompatActivity {

    @BindView(R.id.id)
    EditText mId;

    @BindView(R.id.name)
    EditText mName;

    @BindView(R.id.type)
    EditText mType;

    @BindView(R.id.supplierId)
    EditText mSupplierId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_or_update);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.commit, R.id.update, R.id.clear_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.commit:
                ApiRequest.getInstance()
                        .addRawMeterial(Integer.valueOf(mId.getText().toString().trim()), mName.getText().toString().trim(), mType.getText().toString().trim(), Integer.valueOf(mSupplierId.getText().toString().trim()))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .unsubscribeOn(Schedulers.io())
                        .subscribe(new Subscriber<Object>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {
                                e.printStackTrace();
                                Log.d("RAW", e.getMessage());
                            }

                            @Override
                            public void onNext(Object result) {
                                try {
                                    JSONObject json = new JSONObject(result.toString());
                                    int code = json.optInt("code");
                                    Log.d("RAW", "点击新增， 服务器返回 code ---> " + code);
                                    Toast.makeText(AddOrUpdateActivity.this, "服务器返回 --->" + code, Toast.LENGTH_SHORT).show();
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                break;
            case R.id.update:
                ApiRequest.getInstance()
                        .updateRawMeterial(Integer.valueOf(mId.getText().toString().trim()), mName.getText().toString().trim(), mType.getText().toString().trim(), Integer.valueOf(mSupplierId.getText().toString().trim()))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .unsubscribeOn(Schedulers.io())
                        .subscribe(new Subscriber<Object>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {
                                e.printStackTrace();
                                Log.d("RAW", e.getMessage());
                            }

                            @Override
                            public void onNext(Object result) {
                                try {
                                    JSONObject json = new JSONObject(result.toString());
                                    int code = json.optInt("code");
                                    Log.d("RAW", "点击更新， 服务器返回 code ---> " + code);
                                    Toast.makeText(AddOrUpdateActivity.this, "服务器返回 --->" + code, Toast.LENGTH_SHORT).show();
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                break;
            case R.id.clear_text:
                if (!mId.getText().toString().isEmpty()) {
                    mId.setText("");
                }

                if (!mName.getText().toString().isEmpty()) {
                    mName.setText("");
                }

                if (!mType.getText().toString().isEmpty()) {
                    mType.setText("");
                }

                if (!mSupplierId.getText().toString().isEmpty()) {
                    mSupplierId.setText("");
                }
                break;
        }
    }
}
